<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use backend\models\Product;
use backend\models\FilesForm;
use backend\models\Files;
use backend\models\Category;
use yii\helpers\ArrayHelper;
use backend\models\Goods;
use backend\models\Human;
use backend\models\Article;
use backend\models\ProductItem;
class ProductController extends BaseController{
	public $nodeName = '项目管理';
	public $init_upload_image = '/common/uploads/system/upload.jpg';

	public function init(){
        parent::init();
	}

	public function actionIndex(){
		
	}

	public function actionLists(){
		$lists = Product::find()->asArray()->all();
		return $this->render('lists', ['lists' => $lists]);
	}

	public function actionCreate(){
		$id = $this->request->get('id');

		if(!empty($id)){
			$product = Product::findOne($id);
			$product->setScenario('update');
		}else{
			$product = new Product;
			$product->setScenario('create');
		}

		if($this->request->isPost){
			if($product && $product->load($this->request->post())){
				$product->toMyData();
				if($product->save()){
					$this->session->setFlash('success', '保存成功!');
					return $this->goBack();
				}else{
					$this->session->setFlash('error', '保存失败!');
				}
			}else{
				$this->session('error', '???????!!!!!');
			}

		}

		$result = [
			'product' => $product,
			'pageTitle' => '创建项目',
			'slists' => Category::find()->asArray()->all(),
		];

		return $this->render('data-form', $result);
	}

	public function actionEdit(){

	}

	public function actionDel(){

	}

	public function actionAjaxDel(){

	}

	public function actionAjaxGetOne(){

	}

	public function actionAjaxGetList(){

	}

	/**
	 * 
	 */
	public function actionConfiger(){
		$product_id = $this->request->get('id');
		//通过id初始化

		$product_id = 1;
		$product = new \backend\models\Configer;

		$product->goods = ArrayHelper::getColumn(ProductItem::find()->select('item_id')->where([
			'product_id' => $product_id,
			'type' => 'goods',
			])->asArray()->all(), 'item_id');

		$product->human = ArrayHelper::getColumn(ProductItem::find()->select('item_id')->where([
			'product_id' => $product_id,
			'type' => 'human',
			])->asArray()->all(), 'item_id');

		$product->article = ArrayHelper::getColumn(ProductItem::find()->select('item_id')->where([
			'product_id' => $product_id,
			'type' => 'article',
			])->asArray()->all(), 'item_id');

		$goodsElder = ArrayHelper::getColumn(Goods::find()->select('name')->where(['id' => $product->goods])->asArray()->all(), 'name');
		$humanElder = ArrayHelper::getColumn(Human::find()->select('name')->where(['id' => $product->human])->asArray()->all(), 'name');
		$articleElder = ArrayHelper::getColumn(Article::find()->select('title')->where(['id' => $product->article])->asArray()->all(), 'title');
		
		$result = [
			'product' => $product,
			'pageTitle' => '项目配置',
			'goodsElder' => $goodsElder,
			'humanElder' => $humanElder,
			'articleElder' => $articleElder,
		];
		return $this->render('configer', $result);
	}

	public function actionAjaxAddItem(){

	}

	public function actionAjaxDelItem(){

	}
}